package cn.iocoder.an.module.business.controller.admin.repairtask;

import cn.iocoder.an.module.business.controller.admin.repairrequest.vo.RepairRequestPageReqVO;
import cn.iocoder.an.module.business.controller.admin.repairrequest.vo.RepairRequestRespVO;
import cn.iocoder.an.module.business.controller.admin.repairrequest.vo.UpdateRepairRequestStatusReqVO;
import cn.iocoder.an.module.business.dal.dataobject.repairrequest.RepairRequestDO;
import cn.iocoder.an.module.business.service.repairrequest.RepairRequestService;
import com.fhs.core.trans.anno.TransMethodResult;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.business.controller.admin.repairtask.vo.*;
import cn.iocoder.an.module.business.dal.dataobject.repairtask.RepairTaskDO;
import cn.iocoder.an.module.business.service.repairtask.RepairTaskService;

@Tag(name = "管理后台 - 维修任务")
@RestController
@RequestMapping("/business/repair-task")
@Validated
public class RepairTaskController {

    @Resource
    private RepairTaskService repairTaskService;

    @Resource
    private RepairRequestService repairRequestService;


    @PostMapping("/create")
    @Operation(summary = "创建维修任务")
    @PreAuthorize("@ss.hasPermission('business:repair-task:create')")
    public CommonResult<Long> createRepairTask(@Valid @RequestBody RepairTaskSaveReqVO createReqVO) {
        return success(repairTaskService.createRepairTask(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新维修任务")
    @PreAuthorize("@ss.hasPermission('business:repair-task:update')")
    public CommonResult<Boolean> updateRepairTask(@Valid @RequestBody RepairTaskSaveReqVO updateReqVO) {
        repairTaskService.updateRepairTask(updateReqVO);
        return success(true);
    }

    @PutMapping("/updateStatus")
    @Operation(summary = "更新维修申请状态")
    public CommonResult<Boolean> updateRepairRequestStatus(@Valid @RequestBody UpdateRepairRequestStatusReqVO updateReqVO) {
        repairTaskService.updateRepairRequestAndInertTask(updateReqVO);
        return success(true);
    }


    @DeleteMapping("/delete")
    @Operation(summary = "删除维修任务")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:repair-task:delete')")
    public CommonResult<Boolean> deleteRepairTask(@RequestParam("id") Long id) {
        repairTaskService.deleteRepairTask(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除维修任务")
                @PreAuthorize("@ss.hasPermission('business:repair-task:delete')")
    public CommonResult<Boolean> deleteRepairTaskList(@RequestParam("ids") List<Long> ids) {
        repairTaskService.deleteRepairTaskListByIds(ids);
        return success(true);
    }

    @TransMethodResult
    @GetMapping("/getByRepairId")
    @Operation(summary = "根据申请单ID获得维修任务")
    @Parameter(name = "repairId", description = "编号", required = true, example = "1024")
    public CommonResult<RepairTaskRespVO> getRepairTask(@RequestParam("repairId") Long id) {
        RepairTaskDO taskDO = repairTaskService.getRepairTaskByRepairId(id);
        RepairTaskRespVO bean = BeanUtils.toBean(taskDO, RepairTaskRespVO.class);
        if (taskDO == null) {
            return success(bean);
        }
        RepairRequestDO repairRequest = repairRequestService.getRepairRequest(taskDO.getRepairId() != null? taskDO.getRepairId(): null);
        RepairRequestRespVO requestRespVO = BeanUtils.toBean(repairRequest, RepairRequestRespVO.class);
        bean.setRepairRequest(requestRespVO);
        return success(bean);
    }

    @GetMapping("/get")
    @Operation(summary = "获得维修任务")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<RepairTaskRespVO> getRepairTaskByRepairId(@RequestParam("id") Long id) {
        RepairTaskDO repairTask = repairTaskService.getRepairTask(id);
        return success(BeanUtils.toBean(repairTask, RepairTaskRespVO.class));
    }


    @TransMethodResult
    @GetMapping("/statistics")
    @Operation(summary = "获得维修统计")
    public CommonResult<PageResult<RepairRequestRespVO>> getRepairStatistics(RepairRequestPageReqVO pageReqVO) {
        return success(repairTaskService.getRepairStatistics(pageReqVO));
    }


    @TransMethodResult
    @GetMapping("/page")
    @Operation(summary = "获得维修任务分页")
    @PreAuthorize("@ss.hasPermission('business:repair-task:query')")
    public CommonResult<PageResult<RepairTaskRespVO>> getRepairTaskPage(@Valid RepairTaskPageReqVO pageReqVO) {
        PageResult<RepairTaskDO> pageResult = repairTaskService.getRepairTaskPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RepairTaskRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出维修任务 Excel")
    @PreAuthorize("@ss.hasPermission('business:repair-task:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRepairTaskExcel(@Valid RepairTaskPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RepairTaskDO> list = repairTaskService.getRepairTaskPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "维修任务.xls", "数据", RepairTaskRespVO.class,
                        BeanUtils.toBean(list, RepairTaskRespVO.class));
    }

}